package com.ruoyi.web.controller.system.Fdongbei;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.Fdongbei.domain.TbBmd;
import com.ruoyi.system.Fdongbei.domain.TbGuozhong;
import com.ruoyi.system.Fdongbei.domain.TbHmdw;
import com.ruoyi.system.Fdongbei.domain.TbHuayan;
import com.ruoyi.system.Fdongbei.service.ITbBmdService;
import com.ruoyi.system.Fdongbei.service.ITbHmdwService;
import com.ruoyi.system.Fdongbei.service.ITbHuayanService;
import org.bouncycastle.crypto.paddings.TBCPadding;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 白名单Controller
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/system/bmd")
public class TbBmdController extends BaseController
{
    @Autowired
    private ITbBmdService tbBmdService;
    @Autowired
    private ITbHmdwService tbHmdwService;
    @Autowired
    private ITbHuayanService tbHuayanService;
    /**
     * 查询白名单列表
     */
    @PreAuthorize("@ss.hasPermi('system:bmd:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbBmd tbBmd)
    {
        startPage();
        List<TbBmd> list = tbBmdService.selectTbBmdList(tbBmd);
        return getDataTable(list);
    }

    /**
     * 导出白名单列表
     */
    @PreAuthorize("@ss.hasPermi('system:bmd:list')")
    @Log(title = "白名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBmd tbBmd)
    {
        List<TbBmd> list = tbBmdService.selectTbBmdList(tbBmd);
        ExcelUtil<TbBmd> util = new ExcelUtil<TbBmd>(TbBmd.class);
        util.exportExcel(response, list, "白名单数据");
    }

    /**
     * 获取白名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bmd:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbBmdService.selectTbBmdById(id));
    }

    /**
     * 新增白名单
     */
    @PreAuthorize("@ss.hasPermi('system:bmd:list')")
    @Log(title = "白名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbBmd tbBmd)
    {
        TbHmdw tbHmdw = new  TbHmdw();
        tbHmdw.setCarNum(tbBmd.getCarNum());
        List<TbHmdw> list = tbHmdwService.selectTbHmdList(tbHmdw);
        if (list != null && !list.isEmpty()){
            return error("当前黑名单中已包含此车辆！");
        }
        List<TbBmd> list2 = tbBmdService.selectTbBmdList(tbBmd);
        if(list2 != null && !list2.isEmpty()){
            return warn("请勿重复添加!");
        }
        TbGuozhong tbGuozhong = tbHuayanService.selectTbGuozhong(tbBmd.getCarNum());
        if (tbGuozhong !=null){
            return error("此车辆存在过磅记录，无法设置白名单！");

        }
        return toAjax(tbBmdService.insertTbBmd(tbBmd));

    }

    /**
     * 修改白名单
     */
    @PreAuthorize("@ss.hasPermi('system:bmd:list')")
    @Log(title = "白名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbBmd tbBmd)
    {

        TbHmdw tbHmdw = new  TbHmdw();
        tbHmdw.setCarNum(tbBmd.getCarNum());
        List<TbHmdw> list = tbHmdwService.selectTbHmdList(tbHmdw);
        List<TbBmd> list2 = tbBmdService.selectTbBmdList(tbBmd);
        if (list != null && !list.isEmpty()){
            return error("当前黑名单中已包含此车辆！");
        }
        if(list2 != null && !list2.isEmpty()){
            return warn("此车号已存在!");
        }
        TbGuozhong tbGuozhong = tbHuayanService.selectTbGuozhong(tbBmd.getCarNum());
        if (tbGuozhong !=null){
            return error("此车辆存在过磅记录，无法设置白名单！");

        }
        return toAjax(tbBmdService.updateTbBmd(tbBmd));


    }

    /**
     * 删除白名单
     */
    @PreAuthorize("@ss.hasPermi('system:bmd:list')")
    @Log(title = "白名单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbBmdService.deleteTbBmdByIds(ids));
    }
}
